package com.yz.xyt.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yz.xyt.domain.Department;
import com.yz.xyt.domain.vo.ClazzVo;
import com.yz.xyt.domain.vo.DepartmentVo;
import com.yz.xyt.mapper.DepartmentMapper;
import com.yz.xyt.service.IDepartmentService;
import com.yz.xyt.utils.DateUtils;
import com.yz.xyt.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    public int deleteByPrimaryKey(Integer sid) {
        return departmentMapper.deleteByPrimaryKey(sid);
    }

    @Transactional
    @Override
    public int insert(Department record) {
        return departmentMapper.insert(record);
    }

    @Transactional
    @Override
    public int insertSelective(Department record) {
        return departmentMapper.insertSelective(record);
    }

    @Override
    public Department selectByPrimaryKey(Integer sid) {
        return departmentMapper.selectByPrimaryKey(sid);
    }

    @Override
    public Department selectByDeptNumber(String deptNumber) {
        return departmentMapper.selectByDeptNumber(deptNumber);
    }

    @Transactional
    @Override
    public int updateByPrimaryKeySelective(Department record) {
        return departmentMapper.updateByPrimaryKeySelective(record);
    }

    @Transactional
    @Override
    public int updateByPrimaryKey(Department record) {
        return departmentMapper.updateByPrimaryKey(record);
    }

    @Transactional
    @Override
    public int updateByDeptNumberSelective(Department record) {
        return departmentMapper.updateByDeptNumberSelective(record);
    }
    
    @Transactional
    public void syncAddDepartment(List<Department> list){
        //过滤重复数据
        List<Department> departmentList = new ArrayList<>();
        list.stream().forEach(department -> {
            Department record = selectByDeptNumber(department.getDeptNumber());
            if(record == null){
                departmentList.add(department);
            }
        });
        //新增部门信息
        departmentList.stream().peek(department -> {
            department.setCreateBy(SecurityUtils.getUsername());
            department.setCreateDate(DateUtils.getNowDate());
            System.out.println(DateUtils.getNowDate());
        }).collect(Collectors.toList()).stream().forEach(this::insertSelective);

    }

    /**
     * 同步更新
     * @param list
     */
    @Transactional
    @Override
    public void syncUpdateDepartment(List<Department> list) {
        //更新班级信息
        list.stream().peek(department -> {
            department.setUpdateBy(SecurityUtils.getUsername());
            department.setUpdateDate(DateUtils.getNowDate());
        }).collect(Collectors.toList()).forEach(this::updateByDeptNumberSelective);

    }

    /**
     * 同步删除
     * @param list
     */
    @Transactional
    @Override
    public void syncDeleteDepartment(List<Department> list) {
        list.stream().forEach(department -> {
            departmentMapper.deleteByDeptNumber(department.getDeptNumber());
        });
    }

    @Override
    public String findDeptNumberByName(String deptName) {
        return departmentMapper.findDeptNumberByName(deptName);
    }

    /**
     * 部门分页查询
     * @param departmentVo
     * @return
     */
    @Override
    public PageInfo findDepartmentForPage(DepartmentVo departmentVo) {
        PageHelper.startPage(departmentVo.getPageNumber(),departmentVo.getPageSize());
        List<Department> departmentList = departmentMapper.findDepartmentList(departmentVo);
        return new PageInfo(departmentList);
    }

    /**
     * 部门列表查询
     * @param departmentVo
     * @return
     */
    @Override
    public List<Department> findDepartmentList(DepartmentVo departmentVo) {
        return departmentMapper.findDepartmentList(departmentVo);
    }
}
